import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;

import java.util.List;

public class Main {

    // 不会执行
    public static void main(String[] args) throws Exception {
        System.out.println("Here is MyAgent Main args:" + args[0]);
        List<VirtualMachineDescriptor> list = VirtualMachine.list();
        for (VirtualMachineDescriptor vmd : list) {
            //如果虚拟机的名称为 xxx 则 该虚拟机为目标虚拟机，获取该虚拟机的 pid
            //然后加载 agent.jar 发送给该虚拟机
            System.out.println("name:" + vmd.displayName());
            if (vmd.displayName().contains("UppServerApplication")) {
                System.out.println("attach:" + vmd.displayName() + " vmd:" + vmd.id());
                VirtualMachine virtualMachine = VirtualMachine.attach(vmd.id());
                // F:\javaProject\javaAgent\javaAgent\myAgent\target\myAgent-1.0-SNAPSHOT.jar
                // virtualMachine.loadAgent("D:\\mytest\\java-agent-test\\myAgent\\target\\myAgent-1.0-SNAPSHOT.jar");
                virtualMachine.loadAgent(args[0]);
                virtualMachine.detach();
            }
        }
    }
}
